<template>
  <div id="setting">
    <header>
      <i @click="goBack()"></i>
      <div>设置</div>
    </header>
    <div class="box" v-show='isShowBox' @click.stop='isShowBox=false'>
      <div>
        <label @change="selectClarity" :class="{'active':clarity=='超清','':clarity!=='超清'}">
          <input type="radio" value="超清" v-model="clarity">超清
        </label>
        <label @change="selectClarity" :class="{'active':clarity=='高清','':clarity!=='高清'}">
          <input type="radio" value="高清" v-model="clarity">高清
        </label>
      </div>
      <div>
        <label @change="selectClarity" :class="{'active':clarity=='流畅','':clarity!=='流畅'}">
          <input type="radio" value="流畅" v-model="clarity">流畅
        </label>
      </div>
    </div>
    <ul>
      <li>
        <router-link to="/modifyPassword">
          <i>
            <img src="../../image/per1.png" alt="">
            <h3>修改密码</h3>
          </i>
          <span></span>
        </router-link>
      </li>
      <!-- <li>
        <a @click.false="updateShow=true">
          <img src="../../image/per2.png" alt="">
          <h3>版本信息</h3>
          <b>当前版本{{ versionCode}}</b>
        </a>
      </li> -->
      <li>
        <a @click="checkUpdate">
          <i>
            <img src="../../image/per2.png" alt="">
            <h3>检查新版本</h3>
          </i>
          <i>1.1.0</i>
        </a>
      </li>
      <li>
        <a @click="sureExit">
          <i>
            <img src="../../image/per3.png" alt="">
            <h3>退出登录</h3>
          </i>
        </a>
      </li>
    </ul>
    <div class="Update" v-show="updateShow">
      <div class="progress" @click.false="updateShow=false">
        <div class="bar" :style="{width: barWidth+'%'}"></div>
      </div>
      <div class="down" @click="download()">立即更新版本</div>
    </div>
  </div>
  </div>
</template>
<script>
  import {
    MessageBox
  } from 'mint-ui';
  export default {
    data() {
      return {
        list: [],
        isOpenA: true,
        isOpenB: false,
        isShowBox: false,
        clarity: '流畅',
        barWidth: 0,
        apk: '',
        versionCode: '',
        newVersion: '',
        updateShow: false,
        platform: ''
      }
    },
    components: {

    },
    methods: {
      //下载更新
      update() {
        this.$api.post(this.$api.baseURLSchool, "/admin/version_pro/no_token/1", {}, r => {
          if (r.data.ret == 1) {
            this.apk = r.data.info.url;
            this.newVersion = r.data.info.versionCode;
          }
          if (r.data.ret == -3) {
            localStorage.clear();
            this.isLogin()
          }
        }, err => {
          console.log(err);
        }, false, false)
      },
      // 登录拦截
      isLogin() {
        this.userid = localStorage.getItem("loginMobile");
        if (!this.userid) {
          this.$router.push('/login1')
          return false;
        } else {
          return true;
        }
      },
      //检查更新
      checkUpdate() {
        // var successHandler = function (msg) {
        //   alert("success: " + msg);
        // };
        // var errorHandler = function (msg) {
        //   alert("err: " + msg)
        // };
        //检查更新地址
        var versionCode = '';
        this.platform = device.platform; //Android    iOS
        if (this.platform == 'Android') {
          cordova.getAppVersion.getVersionCode(function (code) {
            // alert('code:' + code);
          })
          cordova.getAppVersion.getVersionNumber(function (version) {
            versionCode = parseInt(version.toString().replace(/\./g, ''));
          });
          this.$api.post(this.$api.baseURLSchool, "/admin/version_pro/no_token/1", {}, r => {
            if (r.data.ret == 1) {
              this.versionCode = versionCode;
              this.newVersion = r.data.info.versionCode;
              if (this.versionCode != '') {
                if (this.versionCode < this.newVersion) {
                  this.updateShow = true
                } else {
                  alert('已是最新版本');
                }
              }
            }
          }, err => {
            console.log(err);
          }, false, false)
        }
      },
      download() {
        var that = this
        var url = this.apk;
        var targetPath = '';
        if (this.platform == 'Android') {
          targetPath = cordova.file.externalDataDirectory + "/bim123.apk";
        }
        //APP下载存放的路径，可以使用cordova file插件进行相关配置 cordova.file.dataDirectory
        var trustHosts = true;
        var options = {};
        var ft = new FileTransfer();
        //监听下载进度
        ft.onprogress = function (e) {
          if (e.lengthComputable) {
            that.barWidth = parseInt((e.loaded / e.total) * 100);
          }
        }
        ft.download(url, targetPath, function (entry) {
          alert('下载成功!');
          that.updateShow = false
          // android打开安装
          if (that.platform == 'Android') {
            var target = entry.fullPath;
            cordova.plugins.fileOpener2.showOpenWithDialog(entry.toInternalURL(),
              'application/vnd.android.package-archive', {
                success: function () {
                  alert('open successfully');
                },
                error: function (e) {
                  // alert('Error status: ' + e.status + ' - Error message: ' + e.message);
                  alert('安装失败');
                }

              });
          } else if (that.platform == 'iOS') {
            // 加载其他应用，应用在升级的时候，iOS使用openURL打开、Android使用open打开  'tel://10086'
            cordova.plugins.fileOpener2.openURL(entry.toURL(), {
              success: function () {
                alert('SUCCESS');
              },
              error: function (e) {
                alert('error');
              }
            });
          }
        }, function (err) {
          alert("下载失败！");
          console.log(err);
        }, true, );
      },
      goBack() {
        this.$router.push({
          path: '/my'
        })
      },
      selectClarity() {
        console.log(this.clarity);
        this.isShowBox = false;
      },
      changeSwitchA() {
        this.isOpenA = !this.isOpenA
      },
      changeSwitchB() {
        this.isOpenB = !this.isOpenB
      },
      sureExit() {
        MessageBox.confirm('确定退出当前账号?').then(action => {
          localStorage.clear()
          this.$router.push({
            path: '/login1'
          });
        }, err => {
          console.log(err);
        });
      }
    },
    created() {
      this.update();
    }
  }

</script>
<style lang="scss">
</style>
